home *** CD-ROM | disk | FTP | other *** search
/ World of Video / World of Video.iso / gfxprograms / 3dprograms / t3dlib / source / writenff.c < prev    next >
C/C++ Source or Header  |  1995-02-13  |  2KB  |  106 lines

  1. /* writenff.c - dump the internal database to an NFF file
  2.  *            - written by Glenn M. Lewis - 10/29/91
  3.  */
  4.  
  5. static char rcs_id[] = "$Id: writenff.c,v 1.9 1993/01/30 12:55:49 glewis Exp $";
  6.  
  7. #include <stdio.h>
  8. #include <ctype.h>
  9. #include "t3dlib.h"
  10. #ifdef __STDC__
  11. #include <stdlib.h>
  12. #include <strings.h>
  13. #include "writenff_protos.h"
  14. #endif
  15.  
  16. static void process_DESC();
  17. static void process_INFO();
  18. static FILE *out;
  19.  
  20. /* Here are a few necessary utilities */
  21.  
  22. static void send_XYZ(f)            /* Print a common string */
  23. XYZ_st *f;
  24. {
  25.     fprintf(out, "%.12g\t%.12g\t%.12g\n", f->x, f->y, f->z);
  26. }
  27.  
  28. static void send_RGB(rgb)            /* Print a common string */
  29. RGB_st *rgb;
  30. {
  31.     fprintf(out, "%.12g\t%.12g\t%.12g\n",
  32.         ((double)rgb->r)/255.0,
  33.         ((double)rgb->g)/255.0,
  34.         ((double)rgb->b)/255.0);
  35. }
  36.  
  37. /********************/
  38. /* The MAIN section */
  39. /********************/
  40.  
  41. int write_NFF(world, file)
  42. WORLD *world;
  43. FILE *file;
  44. {
  45.     register OBJECT *o;
  46.  
  47.     if (!(out = file) || !world) return(0);
  48.  
  49.     if (world->info) process_INFO(world->info);
  50.  
  51.     for (o=world->object; o; o=o->next)
  52.         if (!o->extr) process_DESC(o);
  53.  
  54.     return(1);
  55. }
  56.  
  57. static void process_DESC(object)
  58. OBJECT *object;
  59. {
  60.     register int i;
  61.     register OBJECT *obj;
  62.     register DESC *desc = object->desc;
  63.     register int p1, p2, p3;
  64.  
  65.     /* Process children first */
  66.     for (obj=object->child; obj; obj=obj->next) {
  67.         if (!obj->extr) process_DESC(obj);
  68.     }
  69.  
  70.     if (!desc->pcount || !desc->fcount) return;
  71.  
  72.     fprintf(out, "\n# Start of new object\n");
  73.  
  74.     for (i=0; i<desc->fcount; i++) {
  75.     /* First check to make sure that this triangle is real */
  76.         p1 = desc->edge[(desc->face[i*3])<<1];
  77.         p2 = desc->edge[((desc->face[i*3])<<1)+1];
  78.         if (p1 == p2) continue;    /* How did *this* happen? */
  79.         p3 = desc->edge[(desc->face[i*3+2])<<1];
  80.         if (p1 == p3 || p2 == p3)
  81.             p3 = desc->edge[((desc->face[i*3+2])<<1)+1];
  82.         if (p1 == p3 || p2 == p3) continue; /* How did *this* happen? */
  83.         /* Now check the actual points for equality */
  84.         if (bcmp(&desc->pnts[p1], &desc->pnts[p2], sizeof(XYZ_st))==0 ||
  85.             bcmp(&desc->pnts[p1], &desc->pnts[p3], sizeof(XYZ_st))==0 ||
  86.             bcmp(&desc->pnts[p2], &desc->pnts[p3], sizeof(XYZ_st))==0)
  87.             continue;
  88.  
  89.         fprintf(out, "p 3\n");
  90.         send_XYZ(&desc->pnts[p1]);
  91.         send_XYZ(&desc->pnts[p2]);
  92.         send_XYZ(&desc->pnts[p3]);
  93.     }
  94. }
  95.  
  96. static void process_INFO(info)
  97. INFO *info;
  98. {
  99.     fprintf(out, "v\n");
  100.     if (info->obsv) {
  101.         fprintf(out, "from "); send_XYZ(&info->obsv->came);
  102.     }
  103.     if (info->ambi)
  104.         { fprintf(out, "b "); send_RGB(info->ambi); }
  105. }
  106.